﻿//------------------------------------------------------------------------------ 
// <copyright file="JDatePicker.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
#region Languages
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.ar.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.bg.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.ca.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.cs.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.da.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.de.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.el.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.eo.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.es.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.fa.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.fi.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.fr.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.he.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.hr.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.hu.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.hy.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.id.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.is.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.it.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.ja.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.ko.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.lt.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.lv.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.ms.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.nl.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.no.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.pl.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.pt-BR.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.ro.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.ru.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.sk.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.sl.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.sq.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.sv.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.th.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.tr.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.uk.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.zh-CN.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("FmjQuery.Languages.JDatePicker.zh-TW.js", "text/javascript")]
#endregion

namespace FmjQuery
{
    using System.ComponentModel;
    using System.Text;
    using System.Web.UI;

    /// <summary>
    /// Add DatePicker to TextBox
    /// </summary>
    /// <remarks>
    ///     *  page up/down - previous/next month
    ///     * ctrl+page up/down - previous/next year
    ///     * ctrl+home - current month or open when closed
    ///     * ctrl+left/right - previous/next day
    ///     * ctrl+up/down - previous/next week
    ///     * enter - accept the selected date
    ///     * ctrl+end - close and erase the date
    ///     * escape - close the datepicker without selection 
    /// </remarks>
    [ToolboxData("<{0}:JDatePicker runat=server ShowOn=focus Speed=normal />")]
    [DefaultProperty("TargetControlID")]
    [NonVisualControl]
    public class JDatePicker : JQueryExtendControl, IjLanguages
    {
        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JDatePicker class.
        /// </summary>
        public JDatePicker()
        {
            this.IsRenderControl = false;
        }
        #endregion

        #region Property
        /// <summary>
        /// Gets or sets default language
        /// </summary>
        public string DefaultLanguage
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether default langauge is mandatory
        /// </summary>
        public bool IncludeDefaultLanguage
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether is mandatory
        /// </summary>
        public bool IsMandatory
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether close button is top
        /// </summary>
        public bool CloseAtTop
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether hide prev/next button when not applicable
        /// </summary>
        public bool HideIfNoPrevNext
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the current day link moves to the currently selected date instead of today
        /// </summary>
        public bool GoToCurrent
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the formatDate function is applied to the prevText, nextText, and currentText values before display
        /// </summary>
        public bool NavigationAsDateFormat
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether show DropDownList with list of Month
        /// </summary>
        public bool MonthList
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether show DropDownList with list of Year
        /// </summary>
        public bool YearList
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets minimun year
        /// </summary>
        /// <example>
        ///     absolute : 2000
        ///     relativa : -20 => (Current Year) - 20
        /// </example>
        /// <remarks>
        ///     default value is (Current Year) - 10
        /// </remarks>
        public string MinYear
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets maximun year
        /// </summary>
        /// <example>
        ///     absolute : 2000
        ///     relativa : +20 => (Current Year) + 20
        /// </example>
        /// <remarks>
        ///     default value is (Current Year) + 10
        /// </remarks>
        public string MaxYear
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether user can change first day of the week with one click on header week
        /// </summary>
        public bool ChangeFirstDay
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the entire week row is highlighted when the mouse hovers over a day
        /// </summary>
        public bool HighlightWeek
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether show other month
        /// </summary>
        public bool ShowOtherMonth
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets number of month that view into row
        /// </summary>
        public int NumberOfMonthRow
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets number of month that view into column
        /// </summary>
        public int NumberOfMonthColumn
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets number of month change when click prev/next button
        /// </summary>
        public int StepMonth
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether show number of week
        /// </summary>
        public bool ShowWeekNumber
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether user can select range or single day
        /// </summary>
        public bool SelectRange
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets minimun date
        /// </summary>
        /// <example>
        ///     Set a minimum selectable date via a Date object, or relative to today with a number (e.g. +7) or a string 
        ///     of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '-1y -1m'), or 
        ///     null for no limit.
        /// </example>
        public string MinDate
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets maximun date
        /// </summary>
        /// <example>
        ///     Set a maximum selectable date via a Date object, or relative to today with a number (e.g. +7) or a string 
        ///     of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +1w'), or 
        ///     null for no limit.
        /// </example>
        public string MaxDate
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets date format
        /// </summary>
        /// <remarks>
        ///     *  d - day of month (no leading zero)
        ///     * dd - day of month (two digit)
        ///     * D - day name short
        ///     * DD - day name long
        ///     * m - month of year (no leading zero)
        ///     * mm - month of year (two digit)
        ///     * M - month name short
        ///     * MM - month name long
        ///     * y - year (two digit)
        ///     * yy - year (four digit)
        ///     * @ - Unix timestamp (also known as epoch seconds or POSIX time) in milliseconds since 01/01/1970
        ///     * '...' - literal text
        ///     * '' - single quote
        ///     * anything else - literal text 
        /// </remarks>
        public string DateFormat
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether show a status bar
        /// </summary>
        public bool ShowStatus
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets the function to call to determine the status text for a date within the datepicker
        /// </summary>
        public string ClientStatusForDate
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets the text to display after each date field, e.g. to show the required format
        /// </summary>
        public string AppendText
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets animation speed
        /// </summary>
        [DefaultValue("normal")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public SpeedType Speed
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the datepicker appear automatically when the field receives focus ('focus'), appear only when a button is clicked ('button'), or appear when either event takes place ('both')
        /// </summary>
        [DefaultValue("focus")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public PickerShowOn ShowOn
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets text for button, ShowOn must equals button
        /// </summary>
        public string ButtonText
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets url of image for button
        /// </summary>
        [UrlProperty]
        public string ButtonImageUrl
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to place an image after the field to use as the trigger without it appearing on a button
        /// </summary>
        public bool ButtonImageOnly
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call before show datepicker
        /// </summary>
        /// <example>
        ///     function CustomRange(input)
        /// </example>
        public string ClientBeforeShow
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call before show days
        /// </summary>
        /// <example>
        ///     function NationalDay(date)
        ///     You can use : $.datepicker.noWeekends (that prevents the selection of weekend days)
        /// </example>
        public string ClientBeforeShowDay
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets id of control that update when user select new date
        /// </summary>
        public string AltIDControl
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether alt control is client
        /// </summary>
        public bool IsClientAltControl
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets date format for alt control e. g. dateFormat
        /// </summary>
        public string AltDateFormat
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when user select date
        /// </summary>
        /// <example>
        ///     function changeDate(dateText)
        /// </example>
        public string ClientOnSelect
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when user change year or month
        /// </summary>
        /// <example>
        ///     function changeMonthOrYear(date)
        /// </example>
        public string ClientOnChangeMonthYear
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when user close datepicker
        /// </summary>
        public string ClientOnClose
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the input field is constrained to the current date format
        /// </summary>
        public bool ConstrainInput
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets range separator char
        /// </summary>
        /// <remarks>
        ///     default value is ' - '
        /// </remarks>
        public string RangeSeparator
        {
            get; 
            set;
        }
        #endregion

        #region Overrides of JQueryExtendControl
        /// <summary>
        /// Return list of webResource
        /// </summary>
        /// <returns>web resources</returns>
        public override JWebSources GetResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
            if (this.Enabled)
            {
                this.Manager.DatePicker = true;

                // create script
                StringBuilder builder = new StringBuilder("$(function(){");

                builder.AppendFormat("$('#{0}').datepicker(", this.TargetControlClientID);
                builder.Append("{");
                builder.AppendFormat("duration: '{0}',showOn: '{1}'", this.Speed, this.ShowOn);

                if (this.IsMandatory)
                {
                    builder.Append(",mandatory: true");
                }

                if (this.CloseAtTop)
                {
                    builder.Append(",closeAtTop: true");
                }

                if (this.HideIfNoPrevNext)
                {
                    builder.Append(",hideIfNoPrevNext: true");
                }

                if (this.GoToCurrent)
                {
                    builder.Append(",gotoCurrent: true");
                }

                if (this.NavigationAsDateFormat)
                {
                    builder.Append(",navigationAsDateFormat: true");
                }

                if (this.MonthList)
                {
                    builder.Append(",changeMonth: true");
                }

                // year range
                if (!string.IsNullOrEmpty(this.MinYear) && !string.IsNullOrEmpty(this.MaxYear))
                {
                    string yearRange = string.Concat("'", string.IsNullOrEmpty(this.MinYear) ? "-10" : this.MinYear, ":", string.IsNullOrEmpty(this.MaxYear) ? "+10" : this.MaxYear, "'");
                    builder.AppendFormat(",yearRange: {0}", yearRange);
                }

                if (this.YearList)
                {
                    builder.Append(",changeYear: true");
                }

                if (this.ChangeFirstDay)
                {
                    builder.Append(",changeFirstDay: true");
                }

                if (this.HighlightWeek)
                {
                    builder.Append(",highlightWeek: true");
                }

                if (this.ShowOtherMonth)
                {
                    builder.Append(",showOtherMonths: true");
                }

                if (this.ShowWeekNumber)
                {
                    builder.Append(",showWeeks: true");
                }

                // number of month that show into picker
                if (!this.NumberOfMonthColumn.Equals(0) && !this.NumberOfMonthRow.Equals(0))
                {
                    if (!this.NumberOfMonthColumn.Equals(0) && this.NumberOfMonthRow.Equals(0))
                    {
                        this.NumberOfMonthRow = 1;
                    }

                    if (this.NumberOfMonthColumn.Equals(0) && !this.NumberOfMonthRow.Equals(0))
                    {
                        this.NumberOfMonthColumn = 1;
                    }

                    builder.AppendFormat(",numberOfMonths: [{0},{1}]", this.NumberOfMonthRow, this.NumberOfMonthColumn);
                }

                if (this.StepMonth > 1)
                {
                    builder.AppendFormat(",stepMonths: {0}", this.StepMonth);
                }

                if (this.SelectRange)
                {
                    builder.Append(",rangeSelect: true");
                }

                if (!string.IsNullOrEmpty(this.RangeSeparator))
                {
                    builder.AppendFormat(",rangeSeparator: '{0}'", this.RangeSeparator);
                }

                if (!string.IsNullOrEmpty(this.MinDate))
                {
                    builder.AppendFormat(",minDate: {0}", this.MinDate);
                }

                if (!string.IsNullOrEmpty(this.MaxDate))
                {
                    builder.AppendFormat(",maxDate: {0}", this.MaxDate);
                }

                if (!string.IsNullOrEmpty(this.DateFormat))
                {
                    builder.AppendFormat(",dateFormat: '{0}'", this.DateFormat);
                }

                if (this.ShowStatus)
                {
                    builder.Append(",showStatus: true");
                }

                if (!string.IsNullOrEmpty(this.ClientStatusForDate))
                {
                    builder.AppendFormat(",statusForDate: {0}", this.ClientStatusForDate);
                }

                if (!string.IsNullOrEmpty(this.AppendText))
                {
                    builder.AppendFormat(",appendText: '{0}'", this.AppendText);
                }

                if (!string.IsNullOrEmpty(this.ButtonText))
                {
                    builder.AppendFormat(",buttonText: '{0}'", this.ButtonText);
                }

                if (!string.IsNullOrEmpty(this.ButtonImageUrl))
                {
                    builder.AppendFormat(",buttonImage: '{0}'", this.ResolveUrl(this.ButtonImageUrl));
                }

                if (this.ButtonImageOnly)
                {
                    builder.Append(",buttonImageOnly: true");
                }

                if (!string.IsNullOrEmpty(this.ClientBeforeShow))
                {
                    builder.AppendFormat(",beforeShow: {0}", this.ClientBeforeShow);
                }

                if (!string.IsNullOrEmpty(this.ClientBeforeShowDay))
                {
                    builder.AppendFormat(",beforeShowDay: {0}", this.ClientBeforeShowDay);
                }

                if (!string.IsNullOrEmpty(this.AltIDControl))
                {
                    builder.AppendFormat(",altField: '#{0}'", this.IsClientAltControl ? this.AltIDControl : FindControl(this.AltIDControl).ClientID);
                }

                if (!string.IsNullOrEmpty(this.AltDateFormat))
                {
                    builder.AppendFormat(",altFormat: '{0}'", this.AltDateFormat);
                }

                if (!string.IsNullOrEmpty(this.ClientOnSelect))
                {
                    builder.AppendFormat(",onSelect: {0}", this.ClientOnSelect);
                }

                if (!string.IsNullOrEmpty(this.ClientOnChangeMonthYear))
                {
                    builder.AppendFormat(",onChangeMonthYear: {0}", this.ClientOnChangeMonthYear);
                }

                if (!string.IsNullOrEmpty(this.ClientOnClose))
                {
                    builder.AppendFormat(",onClose: {0}", this.ClientOnClose);
                }

                if (this.ConstrainInput)
                {
                    builder.Append(",constrainInput: true");
                }

                builder.Append("});});");

                Utility.RegisterStartUpScript(this.Page, string.Concat("picker_", this.ClientID), builder.ToString());
            }
        }
        #endregion
    }
}
